package com.ruoyi.web.controller.equip;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.SelectEntity;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.equip.domain.DeviceInfo;
import com.ruoyi.equip.domain.dto.ReaderModelDto;
import com.ruoyi.equip.domain.entity.table.ExportParam;
import com.ruoyi.equip.domain.vo.DeviceInfoVO;
import com.ruoyi.equip.domain.vo.TagVo;
import com.ruoyi.equip.service.IDeviceInfoService;
import com.ruoyi.td.entity.DeviceProperty;
import com.ruoyi.td.entity.DevicePropertyLogQueryBo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/equip/device")
@Api(tags = "读写器相关接口")
public class EquipDeviceController extends BaseController {

    @Autowired
    private IDeviceInfoService deviceInfoService;



    @ApiOperation("查询所有读写器")
    @GetMapping("/list")
    public  TableDataInfo list(DeviceInfoVO deviceInfoVO)
    {
        startPage();
        List<DeviceInfoVO> deviceInfos =
                deviceInfoService.selectDeviceInfoList(deviceInfoVO);
        return getDataTable(deviceInfos);
    }

    @ApiOperation("新增读写器")
    @Log(title = "新增读写器", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody DeviceInfo deviceInfo)
    {

        deviceInfo.setCreateBy(getUsername());
        deviceInfo.setCreateTime(new Date());
        return toAjax(deviceInfoService.add(deviceInfo));
    }


    @ApiOperation("修改指定读写器")
    @Log(title = "修改指定读写器", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody DeviceInfo deviceInfo){
        deviceInfo.setUpdateBy(getUsername());
        return toAjax(deviceInfoService.update(deviceInfo));
    }



    @ApiOperation("删除指定读写器")
    @Log(title = "删除指定读写器", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deviceId}")
    public AjaxResult remove(@PathVariable String deviceId) {
        return toAjax(deviceInfoService.deleteById(deviceId));
    }

    @ApiOperation( "批量删除读写器")
    @Log(title = "批量删除读写器", businessType = BusinessType.DELETE)
    @PostMapping("/batchDel")
    public void batchDel( @RequestBody ExportParam exportParam) {
        deviceInfoService.batchDel(exportParam.getDeviceIds());
    }

    @ApiOperation("获取指定读写器详情")
    @GetMapping("/getDevicesDetail/{id}")
    public AjaxResult getDevicesDetail(@PathVariable String id){
        return success(deviceInfoService.getDevicesDetail(id));
    }


    @ApiOperation("复位网关设备")
    @GetMapping("/resetDevice/{sbId}")
    public AjaxResult resetDevice(@PathVariable String sbId){
        return success(deviceInfoService.resetDevice(sbId));
    }


    @ApiOperation("获取读写器属性历史数据")
    @PostMapping("/deviceProperty/log/list")
    public  AjaxResult  getPropertyHistory(@Validated @RequestBody
                                                   DevicePropertyLogQueryBo logQueryBo) {
         return success(deviceInfoService.getPropertyHistory(logQueryBo));
    }


    @ApiOperation("查询所有读写器下拉框")
    @GetMapping("/selectList")
    public AjaxResult selectList() {
        List<SelectEntity> selectList = deviceInfoService.selectList();
        return success(selectList);
    }


    @ApiOperation("获取指定读写器物模型")
    @GetMapping("/getThingModelOfId/{deviceId}")
    public AjaxResult getThingModelOfId(@PathVariable String deviceId){
        return success(deviceInfoService.getThingModelOfId(deviceId));
    }


    @ApiOperation("查询所有读写器经纬度")
    @GetMapping("/getGis")
    public AjaxResult getGis(){
        return success(deviceInfoService.getGis());
    }



    @ApiOperation("查询指定读写器的最后一次上传数据")
    @GetMapping("/getLastData/{deviceId}")
    public AjaxResult getLastData(@PathVariable String deviceId){
        return success(deviceInfoService.getLastData(deviceId));
    }


    @ApiOperation("读写器导出")
    @Log(title = "读写器管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExportParam exportParam) {
        deviceInfoService.export(response,exportParam);
    }



    @ApiOperation("查询指定读写器的子标签")
    @GetMapping("/child")
    public TableDataInfo getChild(DeviceInfoVO deviceInfoVO){

        startPage();
        List<TagVo> deviceInfos =
                deviceInfoService.getChild(deviceInfoVO.getId());
        return getDataTable(deviceInfos);
    }

}
